package com.hake.core.support.web;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hake.core.support.HttpCode;

public class BaseController {
	
	/** 视图前缀 */
	protected String viewPrefix;
	
	public BaseController() {
		setViewPrefix(defaultViewPrefix());
	}
	
	/**
	 * 设置响应信息-成功
	 * @param modelMap	返回的对象模型
	 * @return
	 */
	public ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap) {
		return setSuccessModelMap(modelMap, null) ;
	}
	
	/**
	 * 设置响应信息-成功
	 * @param modelMap	返回的对象模型
	 * @param data		返回的数据
	 * @return
	 */
	public ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap, Object data) {
		return setModelMap(modelMap, data, HttpCode.OK, null) ;
	}
	
	/**
	 * 设置响应信息-失败
	 * @param modelMap	返回的对象模型
	 * @param msg		返回的提示信息
	 * @return
	 */
	public ResponseEntity<ModelMap> setFailedModelMap(ModelMap modelMap, String msg) {
		return setModelMap(modelMap, null, HttpCode.INTERNAL_SERVER_ERROR, msg) ;
	}
	
	/**
	 * 设置响应信息
	 * @param modelMap	返回的对象模型
	 * @param httpCode 	返回的自定义HttpCode
	 * @return
	 */
	public ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode httpCode) {
		return setModelMap(modelMap, null, httpCode, null) ;
	}
	
	/**
	 * 设置响应信息
	 * @param modelMap	返回的对象模型
	 * @param httpCode	返回的自定义HttpCode
	 * @param msg		返回的提示信息
	 * @param data		返回的数据
	 * @return
	 */
	public ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, Object data, HttpCode httpCode, String msg) {
		modelMap.remove("void");
		if(null != data) {
			modelMap.put("data", data) ;
		}
		
		modelMap.put("httpCode", httpCode.value());
		modelMap.put("msg", (null != msg && !"".equals(msg.trim()) ? msg : httpCode.msg()));
		modelMap.put("timestamp", System.currentTimeMillis()) ;
		
		return ResponseEntity.ok(modelMap) ;
	}
	
	/**
	 * 设置响应信息
	 * @param modelMap		返回的对象模型
	 * @param httpStatus	返回Http状态代码
	 * @param msg			返回的提示信息
	 * @return
	 */
	public ResponseEntity<ModelMap> setStatusModelMap(ModelMap modelMap, HttpStatus httpStatus) {
		return setStatusModelMap(modelMap, httpStatus, null, null) ;
	}
	
	/**
	 * 设置响应信息
	 * @param modelMap		返回的对象模型
	 * @param httpStatus	返回Http状态代码
	 * @param msg			返回的提示信息
	 * @return
	 */
	public ResponseEntity<ModelMap> setStatusModelMap(ModelMap modelMap, HttpStatus httpStatus, String msg) {
		return setStatusModelMap(modelMap, httpStatus, null, msg) ;
	}
	
	/**
	 * 设置响应信息
	 * @param modelMap		返回的对象模型
	 * @param httpStatus	返回Http状态代码
	 * @param data			返回的数据
	 * @return
	 */
	public ResponseEntity<ModelMap> setStatusModelMap(ModelMap modelMap, HttpStatus httpStatus, Object data) {
		return setStatusModelMap(modelMap, httpStatus, data, null) ;
	}
	
	/**
	 * 设置响应信息
	 * @param modelMap		返回的对象模型
	 * @param httpStatus	返回Http状态代码
	 * @param data			返回的数据
	 * @param msg			返回的提示信息
	 * @return
	 */
	public ResponseEntity<ModelMap> setStatusModelMap(ModelMap modelMap, HttpStatus httpStatus, Object data, String msg) {
		modelMap.clear();
		if(null != data) {
			modelMap.put("data", data) ;
		}
		modelMap.put("httpCode", httpStatus.value());
		modelMap.put("msg", (null != msg && !"".equals(msg.trim()) ? msg : HttpCode.INFO.msg(httpStatus.value())));
		modelMap.put("timestamp", System.currentTimeMillis()) ;
		
		return new ResponseEntity<ModelMap>(modelMap, httpStatus) ;
	}
	

	/**
     * 获取Controller注解上的路径
     * @return
     */
    protected String defaultViewPrefix() {
        String currentViewPrefix = "";
        RequestMapping requestMapping = AnnotationUtils.findAnnotation(getClass(), RequestMapping.class);
        if (requestMapping != null && requestMapping.value().length > 0) {
            currentViewPrefix = requestMapping.value()[0];
        }
        return currentViewPrefix;
    }
    /**
     * 设置视图名称
     * @param suffixName
     * @return
     */
	public String viewName(String suffixName) {
        if (!suffixName.startsWith("/")) {
            suffixName = "/" + suffixName;
        }
        return getViewPrefix() + suffixName;
    }
	/**
	 * 设置视图前缀
	 * @param viewPrefix
	 */
	public void setViewPrefix(String viewPrefix) {
        if (viewPrefix.startsWith("/")) {
            viewPrefix = viewPrefix.substring(1);
        }
        this.viewPrefix = viewPrefix;
    }

    public String getViewPrefix() {
        return viewPrefix;
    }
	
}
